﻿Public Class oPromotionHeader
    Public Function UpdateJsonPromotionHeader(ByVal PromotionID As String _
                                              , ByVal CompanyCode As String _
                                               , ByVal PromotionCode As String _
                                               , ByVal PromotionName As String _
                                               , ByVal FatherPromotionCode As String _
                                               , ByVal PromotionType As String _
                                               , ByVal FromDate As DateTime _
                                               , ByVal ToDate As DateTime _
                                               , ByVal FromTime As DateTime _
                                               , ByVal ToTime As DateTime _
                                               , ByVal IsActive As Boolean _
                                               , ByVal IsOpional As Boolean _
                                               , ByVal FromAmount As Double _
                                               , ByVal ToAmount As Double _
                                               , ByVal FromQuantity As Double _
                                               , ByVal ToQuantity As Double _
                                               , ByVal DiscountType As String _
                                               , ByVal DiscountValue As Double _
                                               , ByVal MaxCountApply As Double _
                                               , ByVal Monday As Boolean _
                                               , ByVal MondayStart As DateTime _
                                               , ByVal MondayEnd As DateTime _
                                               , ByVal Tuesday As Boolean _
                                               , ByVal TuesdayStart As DateTime _
                                               , ByVal TuesdayEnd As DateTime _
                                               , ByVal Wednesday As Boolean _
                                               , ByVal WednesdayStart As DateTime _
                                               , ByVal WednesdayEnd As DateTime _
                                               , ByVal Thursday As Boolean _
                                               , ByVal ThursdayStart As DateTime _
                                               , ByVal ThursdayEnd As DateTime _
                                               , ByVal Friday As Boolean _
                                               , ByVal FridayStart As DateTime _
                                               , ByVal FridayEnd As DateTime _
                                               , ByVal Saturday As Boolean _
                                               , ByVal SaturdayStart As DateTime _
                                               , ByVal SaturdayEnd As DateTime _
                                               , ByVal Sunday As Boolean _
                                               , ByVal SundayStart As DateTime _
                                               , ByVal SundayEnd As DateTime _
                                               , ByVal CreatedUserID As String _
                                               , ByVal CreatedDate As DateTime _
                                               , ByVal BudgetAmount As Double _
                                               , ByVal TargetRevenue As Double _
                                               , ByVal EveryAt As Integer _
                                               , ByVal databaseName As String, ByVal isInsert As Boolean) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(databaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {CompanyCode _
                                                   , PromotionCode _
                                                   , PromotionName _
                                                   , FatherPromotionCode _
                                                   , PromotionType _
                                                   , FromDate _
                                                   , ToDate _
                                                   , FromTime _
                                                   , ToTime _
                                                   , IsActive _
                                                   , IsOpional _
                                                   , FromAmount _
                                                   , ToAmount _
                                                   , FromQuantity _
                                                   , ToQuantity _
                                                   , DiscountType _
                                                   , DiscountValue _
                                                   , MaxCountApply _
                                                   , Monday _
                                                   , MondayStart _
                                                   , MondayEnd _
                                                   , Tuesday _
                                                   , TuesdayStart _
                                                   , TuesdayEnd _
                                                   , Wednesday _
                                                   , WednesdayStart _
                                                   , WednesdayEnd _
                                                   , Thursday _
                                                   , ThursdayStart _
                                                   , ThursdayEnd _
                                                   , Friday _
                                                   , FridayStart _
                                                   , FridayEnd _
                                                   , Saturday _
                                                   , SaturdayStart _
                                                   , SaturdayEnd _
                                                   , Sunday _
                                                   , SundayStart _
                                                   , SundayEnd _
                                                   , CreatedUserID _
                                                   , CreatedDate _
                                                   , BudgetAmount _
                                                   , TargetRevenue _
                                                   , EveryAt _
                                                }
                Dim query As New StringBuilder
                If isInsert = True Then
                    query.Append("INSERT INTO [oPromotionHeader] ")
                    query.Append(" ([CompanyCode]")
                    query.Append(" ,[PromotionCode]")
                    query.Append(" ,[PromotionName]")
                    query.Append(" ,[FatherPromotionCode]")
                    query.Append(" ,[PromotionType]")
                    query.Append(" ,[FromDate]")
                    query.Append(" ,[ToDate]")
                    query.Append(" ,[FromTime]")
                    query.Append(" ,[ToTime]")
                    query.Append(" ,[IsActive]")
                    query.Append(" ,[IsOpional]")
                    query.Append(" ,[FromAmount]")
                    query.Append(" ,[ToAmount]")
                    query.Append(" ,[FromQuantity]")
                    query.Append(" ,[ToQuantity]")
                    query.Append(" ,[DiscountType]")
                    query.Append(" ,[DiscountValue]")
                    query.Append(" ,[MaxCountApply]")
                    query.Append(" ,[Monday]")
                    query.Append(" ,[MondayStart]")
                    query.Append(" ,[MondayEnd]")
                    query.Append(" ,[Tuesday]")
                    query.Append(" ,[TuesdayStart]")
                    query.Append(" ,[TuesdayEnd]")
                    query.Append(" ,[Wednesday]")
                    query.Append(" ,[WednesdayStart]")
                    query.Append(" ,[WednesdayEnd]")
                    query.Append(" ,[Thursday]")
                    query.Append(" ,[ThursdayStart]")
                    query.Append(" ,[ThursdayEnd]")
                    query.Append(" ,[Friday]")
                    query.Append(" ,[FridayStart]")
                    query.Append(" ,[FridayEnd]")
                    query.Append(" ,[Saturday]")
                    query.Append(" ,[SaturdayStart]")
                    query.Append(" ,[SaturdayEnd]")
                    query.Append(" ,[Sunday]")
                    query.Append(" ,[SundayStart]")
                    query.Append(" ,[SundayEnd]")
                    query.Append(" ,[CreatedUserID]")
                    query.Append(" ,[CreatedDate]")
                    query.Append(" ,[BudgetAmount]")
                    query.Append(" ,[TargetRevenue]")
                    query.Append(" ,[EveryAt])")
                    query.Append(" VALUES")
                    query.Append(" (@Param1")
                    query.Append(" ,@Param2")
                    query.Append(" ,@Param3")
                    query.Append(" ,@Param4")
                    query.Append(" ,@Param5")
                    query.Append(" ,@Param6")
                    query.Append(" ,@Param7")
                    query.Append(" ,@Param8")
                    query.Append(" ,@Param9")
                    query.Append(" ,@Param10")
                    query.Append(" ,@Param11")
                    query.Append(" ,@Param12")
                    query.Append(" ,@Param13")
                    query.Append(" ,@Param14")
                    query.Append(" ,@Param15")
                    query.Append(" ,@Param16")
                    query.Append(" ,@Param17")
                    query.Append(" ,@Param18")
                    query.Append(" ,@Param19")
                    query.Append(" ,@Param20")
                    query.Append(" ,@Param21")
                    query.Append(" ,@Param22")
                    query.Append(" ,@Param23")
                    query.Append(" ,@Param24")
                    query.Append(" ,@Param25")
                    query.Append(" ,@Param26")
                    query.Append(" ,@Param27")
                    query.Append(" ,@Param28")
                    query.Append(" ,@Param29")
                    query.Append(" ,@Param30")
                    query.Append(" ,@Param31")
                    query.Append(" ,@Param32")
                    query.Append(" ,@Param33")
                    query.Append(" ,@Param34")
                    query.Append(" ,@Param35")
                    query.Append(" ,@Param36")
                    query.Append(" ,@Param37")
                    query.Append(" ,@Param38")
                    query.Append(" ,@Param39")
                    query.Append(" ,@Param40")
                    query.Append(" ,@Param41")
                    query.Append(" ,@Param42")
                    query.Append(" ,@Param43")
                    query.Append(" ,@Param44)")
                    Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
                    If count > 0 Then
                        'Get Max Id
                        Dim ds As DataSet = connect.ObjectGetAll_Query_SAP("SELECT IDENT_CURRENT('oPromotionHeader')")
                        If ds IsNot Nothing And ds.Tables.Count > 0 Then
                            Dim newDocEntry As Integer = Integer.Parse(ds.Tables(0).Rows(0)(0).ToString())
                            Return newDocEntry
                        End If
                    End If
                Else
                    query.Append(" UPDATE [oPromotionHeader]")
                    query.Append(" SET [CompanyCode] = @Param1")
                    query.Append(" ,[PromotionCode] = @Param2")
                    query.Append(" ,[PromotionName] = @Param3")
                    query.Append(" ,[FatherPromotionCode] = @Param4")
                    query.Append(" ,[PromotionType] = @Param5")
                    query.Append(" ,[FromDate] = @Param6")
                    query.Append(" ,[ToDate] = @Param7")
                    query.Append(" ,[FromTime] = @Param8")
                    query.Append(" ,[ToTime] = @Param9")
                    query.Append(" ,[IsActive] = @Param10")
                    query.Append(" ,[IsOpional] = @Param11")
                    query.Append(" ,[FromAmount] = @Param12")
                    query.Append(" ,[ToAmount] = @Param13")
                    query.Append(" ,[FromQuantity] = @Param14")
                    query.Append(" ,[ToQuantity] = @Param15")
                    query.Append(" ,[DiscountType] = @Param16")
                    query.Append(" ,[DiscountValue] = @Param17")
                    query.Append(" ,[MaxCountApply] = @Param18")
                    query.Append(" ,[Monday] = @Param19")
                    query.Append(" ,[MondayStart] = @Param20")
                    query.Append(" ,[MondayEnd] = @Param21")
                    query.Append(" ,[Tuesday] = @Param22")
                    query.Append(" ,[TuesdayStart] = @Param23")
                    query.Append(" ,[TuesdayEnd] = @Param24")
                    query.Append(" ,[Wednesday] = @Param25")
                    query.Append(" ,[WednesdayStart] = @Param26")
                    query.Append(" ,[WednesdayEnd] = @Param27")
                    query.Append(" ,[Thursday] = @Param28")
                    query.Append(" ,[ThursdayStart] = @Param29")
                    query.Append(" ,[ThursdayEnd] = @Param30")
                    query.Append(" ,[Friday] = @Param31")
                    query.Append(" ,[FridayStart] = @Param32")
                    query.Append(" ,[FridayEnd] = @Param33")
                    query.Append(" ,[Saturday] = @Param34")
                    query.Append(" ,[SaturdayStart] = @Param35")
                    query.Append(" ,[SaturdayEnd] = @Param36")
                    query.Append(" ,[Sunday] = @Param37")
                    query.Append(" ,[SundayStart] = @Param38")
                    query.Append(" ,[SundayEnd] = @Param39")
                    query.Append(" ,[BudgetAmount] = @Param40")
                    query.Append(" ,[TargetRevenue] = @Param41")
                    query.Append(" ,[EveryAt] = @Param42")
                    query.Append(" WHERE [PromotionID] = @Param43")
                    connect.Object_Execute_SAP(query.ToString(), New Object() {
                                                    CompanyCode _
                                                   , PromotionCode _
                                                   , PromotionName _
                                                   , FatherPromotionCode _
                                                   , PromotionType _
                                                   , FromDate _
                                                   , ToDate _
                                                   , FromTime _
                                                   , ToTime _
                                                   , IsActive _
                                                   , IsOpional _
                                                   , FromAmount _
                                                   , ToAmount _
                                                   , FromQuantity _
                                                   , ToQuantity _
                                                   , DiscountType _
                                                   , DiscountValue _
                                                   , MaxCountApply _
                                                   , Monday _
                                                   , MondayStart _
                                                   , MondayEnd _
                                                   , Tuesday _
                                                   , TuesdayStart _
                                                   , TuesdayEnd _
                                                   , Wednesday _
                                                   , WednesdayStart _
                                                   , WednesdayEnd _
                                                   , Thursday _
                                                   , ThursdayStart _
                                                   , ThursdayEnd _
                                                   , Friday _
                                                   , FridayStart _
                                                   , FridayEnd _
                                                   , Saturday _
                                                   , SaturdayStart _
                                                   , SaturdayEnd _
                                                   , Sunday _
                                                   , SundayStart _
                                                   , SundayEnd _
                                                   , BudgetAmount _
                                                   , TargetRevenue _
                                                   , EveryAt _
                                                   , PromotionID})
                    Return PromotionID
                End If
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function GetAllPromotionHeader(ByVal dataBaseName As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oPromotionHeader")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oPromotionHeader")
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function GetPromotionHeaderByCompanyCode(ByVal dataBaseName As String, ByVal companyCode As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oPromotionHeader")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oPromotionHeader WHERE CompanyCode = @Param1", New Object() {companyCode})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function GetPromotionHeaderByID(ByVal dataBaseName As String, ByVal promotionID As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oPromotionHeader")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oPromotionHeader WHERE PromotionID = @Param1", New Object() {promotionID})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function GetLastRecordPromotionHeader(ByVal dataBaseName As String, ByVal companyCode As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oPromotionHeader")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                ds = connect.ObjectGetAll_Query_SAP("SELECT TOP 1 * FROM oPromotionHeader T0 JOIN oCompany T1 ON T0.CompanyCode  = T1.CompanyCode WHERE T0.CompanyCode  = @Param1 ORDER BY PromotionID DESC", New Object() {companyCode})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
End Class
